import subprocess
import sys

# 设置标准输出编码为 UTF-8
sys.stdout.reconfigure(encoding='utf-8')


def release_and_renew_ip_windows():
    try:
        # 更改控制台代码页为 UTF-8
        subprocess.run(["chcp", "65001"], shell=True, check=True)

        # 释放当前 IP 配置
        subprocess.run(["ipconfig", "/release"], check=True)
        print("已成功释放 IP 地址")

        # 重新获取 IP 配置
        subprocess.run(["ipconfig", "/renew"], check=True)
        print("已成功重新获取 IP 地址")

    except subprocess.CalledProcessError as e:
        print(f"执行命令时出错: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")


# 执行函数
release_and_renew_ip_windows()